local LevelFactory = {}
LevelFactory.levelTilesMap = {
	'resources.maps.Map01',
	'resources.maps.Map02',
	'resources.maps.Map03',
	'resources.maps.MapTest64',
}
LevelFactory.positions = {
	{
		startPosition = {row=9, col=4},
		goal = {row= 3, col=48}
	},
	{
		startPosition = {row=9, col=4},
		goal = {row= 7, col=98}
	},
	{
		startPosition = {row=6, col=4},
		goal = {row= 6, col=98}
	},
	{
		startPosition = {row=7, col=15},
		goal = {row= 6, col=98}
	}
}


function LevelFactory:generateLevel(level)
	local tileMap = {}
	-- print (LevelFactory.levelTilesMap[level])
	local tileInfo = require(LevelFactory.levelTilesMap[level])
	local tileProperties = tileInfo.tilesets[1].tiles
	for i = 1, #tileProperties  do
		local tileItem = tileProperties[i]
		tileMap[tileItem.id] = tileItem.properties
		-- print (tileItem.id,  tileItem.properties.spriteId,  tileItem.properties.type,  tileItem.properties.appearance)
	end
	local tileLayer = tileInfo.layers[1]
	local tileData = tileLayer.data
	local tileRows = tileLayer.height
	local tileCols = tileLayer.width
	local cellMap = {}
	local knights = {}
	local orcs = {}
	local x, y = 1, 1
	local colsMod = 0
	local tileId = -1
	local counter = 1 
	for i = 1, tileRows do
		table.insert(cellMap, {})
		for j = 1, tileCols do
			tileId = tileData[counter]
			counter = counter + 1
			if tileId ~= 0 then
				tileId = tileId - 1
				table.insert(cellMap[i], tileMap[tileId])
				-- print (tileMap[tileId].type)
				if  tileMap[tileId].type == RED_CELL  and tileMap[tileId].appearance ~= 'ai' and   tileMap[tileId].appearance ~= 'ad'  then
					table.insert(knights, {row = i, col = j})
				elseif tileMap[tileId].type == PURPLE_CELL and tileMap[tileId].appearance ~= 'ai' and   tileMap[tileId].appearance ~= 'ad' then
					table.insert(orcs, {row = i, col = j})
				end
			else 
				table.insert(cellMap[i], {appearance = 't'})
			end
		end
	end
	return {
		rows = tileRows,
		cols = tileCols,
		levelMapConfig = cellMap,
		start = LevelFactory.positions[level].startPosition,
		goal = LevelFactory.positions[level].goal,
		knights = knights,
		orcs = orcs
	}

end

return LevelFactory